import {ref, Ref, reactive} from 'vue'

export interface UseBack {
  (options?: UseBackOptions | null | undefined): UseBackReturn
}

export interface UseBackOptions {
  close?: () => void;
  confirme?: () => void;
}

export interface UseBackReturn {
  dialog: any;
  handleDialog: HandleDialog
}

export interface DialogStatus {
  /** 弹窗状态 */
  showDialog: Boolean;
  /** 弹窗内容 */
  content?: String;
  /** 弹窗标题 */
  title?: String;
}

export interface HandleDialog {
  (options: DialogStatus): void
}

const useBack: UseBack = (options) => {
  const {close, confirme} = options || {}
  const dialog = reactive<DialogStatus>({
    showDialog: false,
    content: '默认文本',
    title: '默认标题',
  })

  // 弹窗处理函数
  const handleDialog: HandleDialog = (options) => {
    const {showDialog, content, title} = options || {}
    if(showDialog !== dialog.showDialog) {
      dialog.showDialog = showDialog
    }
    if(content) {
      dialog.content = content
    }
    if(title) {
      dialog.title = title
    }
  }

  return {dialog, handleDialog}
}

export default useBack